'use strict'

define([ 'jquery', 'bootstrap', 'select2', 'shiftform', 'validate',
        'validate.en', 'datetimepicker', 'datetimepickerCN', 'qrcode' ], function($) {
    return {
        init : function(data) {
            this.initDelete();
            this.initPhase();
            this.initInputResult();
            this.initSearch(data.subCategory);
        },
            
              
              
        initSearch: function(subCategory) {
              
            $(document).on('shown.bs.modal', '#search', function(e) {
              $('[name="beginDateFrom"]').datetimepicker({
                format: 'yyyy-mm-dd',
                language: 'zh-CN',
                autoclose: 1,
                clearBtn:true,//清除按钮
                minView: 2,
                forceParse: 0
              });
              
              var $eventSelect1 = $("select[name='firstCategory");
            	$('select[name="firstCategory"]').select2({
      		      theme: "classic",
      		      placeholder : '请选择一级分类',
      		      minimumResultsForSearch : -1,
      		      allowClear : true
      		    });
            	
            	$('[name="secondCategory"]').prop("disabled", true);
            	
            	$eventSelect1.on("change" ,function(e) {
            		console.log(e.val)
            		console.log(subCategory)
            		var item = subCategory[e.val]
            		console.log(item)
            		$('input[name="secondCategory"]').prop("disabled", false);
            		$('input[name="secondCategory"]').val(null).trigger('change');
            		$('input[name="secondCategory"]').select2({
            			theme: "classic",
          		        placeholder : '请选择二级分类',
          		        minimumResultsForSearch : 1,
          		        allowClear : true,
            			data: item
            		})
            	})
            	
              
              $('[name="beginDateTo"]').datetimepicker({
                  format: 'yyyy-mm-dd',
                  language: 'zh-CN',
                  autoclose: 1,
                  clearBtn:true,//清除按钮
                  minView: 2,
                  forceParse: 0
                });
              
              $('[name="endDateFrom"]').datetimepicker({
                  format: 'yyyy-mm-dd',
                  language: 'zh-CN',
                  autoclose: 1,
                  clearBtn:true,//清除按钮
                  minView: 2,
                  forceParse: 0
                });
            
            $('[name="endDateTo"]').datetimepicker({
                format: 'yyyy-mm-dd',
                language: 'zh-CN',
                autoclose: 1,
                clearBtn:true,//清除按钮
                minView: 2,
                forceParse: 0
              });
          });
            
        },
         initPhase : function() {

            $(document).on('shown.bs.modal', '#phase', function(e) {

               $(document).find('form[data-validate="true"]').bt_validate() // bind
                                                                                // validate
              $('[name="beginDate"]').datetimepicker({
                  format: 'yyyy-mm-dd',
                  language: 'zh-CN',
                  autoclose: 1,
                  clearBtn:true,//清除按钮
                  minView: 2,
                  forceParse: 0
                });
                $('[name="endDate"]').datetimepicker({
                    format: 'yyyy-mm-dd',
                    language: 'zh-CN',
                    autoclose: 1,
                    clearBtn:true,//清除按钮
                    minView: 2,
                    forceParse: 0
                  });
                  
                var $form = $(document).find("form") // bind shiftform
                $form.shiftform({
                    beforeSubmit : function(el) {
                        $form.trigger("validate.ajax.submit");
                        return $.bt_validate.result[$.bt_validate.form_id]
                    }
                })
            });

        },        
         initInputResult : function() {

            $(document).on('shown.bs.modal', '#inputResult', function(e) {

               $(document).find('form[data-validate="true"]').bt_validate() // bind
           
                var $form = $(document).find("form") // bind shiftform
                $form.shiftform({
                    beforeSubmit : function(el) {
                        $form.trigger("validate.ajax.submit");
                        return $.bt_validate.result[$.bt_validate.form_id]
                    }
                })
            });

        },        
        initDelete : function() {

            $(document).on('shown.bs.modal', '#delete', function(e) {

                $(document).find('form[data-validate="true"]').bt_validate() // bind
                                                                                // validate

                var $form = $(document).find("form") // bind shiftform
                $form.shiftform({
                    beforeSubmit : function(el) {
                        $form.trigger("validate.ajax.submit");
                        return $.bt_validate.result[$.bt_validate.form_id]
                    }
                })
            });

        }

    }
})
